import productItemRow from '@/components/product-item/components/product-item-row.vue'
import { createAndDeleteCollection, clearCollection, fetchCollectionList} from '@/api/collection.js';

export default {
	components: {
		productItemRow
	},
	data() {
		return {
			//右侧滑动
			options:[{
				text: '删除',
				style: {
					backgroundColor: '#dd524d'
				}
			}],
			loadingType: 'more',
			productList: [],
			pageParam: {
				pageNum: 0,
				pageSize: 5
			},
		};
	},
	onLoad(options) {
		this.loadData();
	},
	//下拉刷新
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	//加载更多
	onReachBottom() {
		this.pageParam.pageNum++;
		this.loadData();
	},
	// #ifndef MP
	onNavigationBarButtonTap(e) {
		const index = e.index;
		let thisObj = this;
		if (index === 0) {
			uni.showModal({
				title: '提示',
				content: '是否要清空所有收藏记录？',
				success: function (res) {
					if (res.confirm) {
						clearCollection().then(response=>{
							thisObj.loadData('refresh');
						});
					}
				}
			});
		}
	},
	// #endif

	methods: {
		//滑动删除
		swipeClick(e, index) {
			let {
				content
			} = e;
			var _this = this;
			if (content.text === '删除') {
				uni.showModal({
					title: '提示',
					content: '是否取消删除',
					success: res => {
						if (res.confirm) {
							var select = this.productList[index];
							let param = {
								productId: select.productId
							}
							createAndDeleteCollection(param).then(response=>{
								_this.loadData('refresh');
							});
						
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			} else {
				uni.showToast({
					title: `点击了${e.content.text}按钮`,
					icon: 'none'
				});
			}
		},
		
		
		//加载商品 ，带下拉刷新和上滑加载
		async loadData(type = 'add') {
			//没有更多直接返回
			if (type === 'add') {
				if (this.loadingType === 'nomore') {
					return;
				}
				this.loadingType = 'loading';
			} else {
				this.loadingType = 'more'
			}

			if (type === 'refresh') {
				this.pageParam.pageNum = 0;
				this.productList = [];
			}
			fetchCollectionList(this.pageParam).then(response => {
				let dataList = response.data.content;
				
				if(response.data.last){
					//没有更多了
					this.loadingType = 'nomore';
				}else{
					this.loadingType = 'more';
				}
				this.productList = this.productList.concat(dataList);
				
				if (type === 'refresh') {
					uni.stopPullDownRefresh();
				}
			});
		},
		
	},
}